﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Threading;

namespace ulc
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    
    public partial class MainWindow : Window
    {
        CbShowStatusDelegate delg0;
        CbSwitchUIPageByTypeDelegate delg1;
        CbUpdateUserListDelegate delg2;
        CbClearnUpUserListDelegate delg3;

        public static void loginfo_msg(String msg) {
            string message = msg;
            string caption = "Alert";
            MessageBoxButton buttons = MessageBoxButton.YesNo;

            MessageBox.Show(message, caption, buttons);
        }
        public static void loginfo(String msg) {
            Console.WriteLine(msg);
        }

        public void CbShowStatus(int astatus_code, IntPtr astatus_str)
        {
            String status_str = Marshal.PtrToStringUni(astatus_str);
            loginfo("auth ret" + astatus_code + ":" + status_str);

        }

        public void CbClearnUpUserList()
        {
        }

        public void CbSwitchUIPageByType(int uipage)
        {
            loginfo("switch ui:" + uipage);
            if(uipage==1)btnJoinMeeting.IsEnabled = true;
            if (uipage == 3) this.Show();


        }

        public void CbUpdateUserList(int index, IntPtr uname, IntPtr uid, bool bAdd)
        {
        }


        clsZoomMgr zoomgr;
        LocalData ld=null;
        public void savedata()
        {
            if (ld == null)
                return;
            ld.MeetingNumbers.Clear();
            ld.MeetingNumbers.Add(cbMeetingNumber.Text);
            foreach (String number in cbMeetingNumber.Items)
            {
                if(number != cbMeetingNumber.Text)
                    ld.MeetingNumbers.Add(number);
            }
            ld.LoginEmail = tbEmail.Text;
            ld.LoginPass = tbPassword.Text;
            ld.LoginMemberMe = cbMemberme.IsChecked.GetValueOrDefault();
            ld.MeetingName = tbYourMeetingName.Text;
            LocalData.SaveToJSON<LocalData>(ld,AppDomain.CurrentDomain.BaseDirectory + "/data.json");
        }
        public void loaddata()
        {
            try
            {
                ld = LocalData.LoadFromJSON<LocalData>(AppDomain.CurrentDomain.BaseDirectory + "/data.json");
            }
            catch
            {
                ld = new LocalData();
            }
            cbMeetingNumber.Items.Clear();
            bool FirstAdded = false;
            foreach (String number in ld.MeetingNumbers) {
                if (!FirstAdded)
                {
                    FirstAdded = true;
                    cbMeetingNumber.Text = number;
                }
                cbMeetingNumber.Items.Add(number);
            }
            tbEmail.Text = ld.LoginEmail;
            tbPassword.Text = ld.LoginPass;
            cbMemberme.IsChecked = ld.LoginMemberMe;
            tbYourMeetingName.Text = ld.MeetingName;

        }


        public MainWindow()
        {
            InitializeComponent();

            btnLogin.Visibility = Visibility.Hidden;
            tbEmail.Visibility = Visibility.Hidden;
            tbPassword.Visibility = Visibility.Hidden;
            cbMemberme.Visibility = Visibility.Hidden;
            btnJoinMeeting.Visibility = Visibility.Visible;
            btnJoinMeeting.IsEnabled = false;
            
            Dispatcher.BeginInvoke(new Action(() => { 
                Trace.WriteLine("DONE!", "Rendering");
                do_init();
            }),
            DispatcherPriority.ContextIdle, null
            );


        }
        public void do_init()
        {
            loaddata();
            if (ld != null && ld.MeetingName == "showmethemoney") {
                ConsoleManager.Show();
            }
            String domain = "https://zoom.us";
            String apikey = "WiUwcF0s0Mo4yJvkXrGOUlVUJgsQvw45IWi7";
            String apisec = "A7Bntc3AzkI2hoEm1h4MApcrHIZgjR3RDKnL";
             delg0 = new CbShowStatusDelegate(CbShowStatus);
             delg1 = new CbSwitchUIPageByTypeDelegate(CbSwitchUIPageByType);
             delg2 = new CbUpdateUserListDelegate(CbUpdateUserList);
             delg3 = new CbClearnUpUserListDelegate(CbClearnUpUserList);


            zoomgr = new clsZoomMgr(
                delg0,
                delg1,
                delg2,
                delg3
                );

            //http://stackoverflow.com/questions/1658269/char-pointer-from-string-in-c-sharp
            unsafe
            {
                fixed (char* pdomain = domain)
                fixed (char* pkey = apikey)
                fixed (char* psec = apisec)
                {
                    zoomgr.init(pdomain, pkey, psec);
                }
            }
        }


        private void btnJoinMeeting_Click(object sender, RoutedEventArgs e)
        {

            String meetingnumber = cbMeetingNumber.Text.Replace("-", "");
            String username = tbYourMeetingName.Text;
            String password = "11";
            String userid = "11";
            savedata();
            unsafe {
                fixed (char* pmeetingnumber = meetingnumber)
                fixed (char* pusername = username)
                fixed (char* ppassword = password)
                fixed (char* puserid = userid)
                {
                  zoomgr.ApiUserStart(pmeetingnumber, pusername, puserid, ppassword);
                  //zoomgr.NormalUserStart(pmeetingnuber);
                }
            }
            this.Hide();
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            savedata();
            unsafe
            {
                fixed (char* pmail = tbEmail.Text)
                fixed (char* ppassword = tbPassword.Text)
                {
                    zoomgr.Login(pmail, ppassword, cbMemberme.IsChecked.GetValueOrDefault());
                }
            }
        }

        private void PART_MAXIMIZE_RESTORE_Click(object sender, RoutedEventArgs e)
        {
            if (this.WindowState == System.Windows.WindowState.Normal)
            {
                this.WindowState = System.Windows.WindowState.Maximized;
            }
            else
            {
                this.WindowState = System.Windows.WindowState.Normal;
            }

        }

        private void PART_MINIMIZE_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Minimized;

        }

        private void PART_CLOSE_Click(object sender, RoutedEventArgs e)
        {
            savedata();
            this.Close();
        }
        private void PART_TITLEBAR_MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            DragMove();


        }

    }
}
